<?php

/**
 * 会员中心
 */
namespace User\Controller;

use Common\Controller\MemberbaseController;
class OrderController extends MemberbaseController {


	function _initialize() {
		parent::_initialize();
			$this->db = D("Order");
		$this->assign ( "seo_title",  "订单中心" );
	}


	protected function page($total, $size = 20, $number = 0, $config = array()) {

		$config=array("prev" => "<", "next" => ">");
		$Page =page($total, $size, $number, $config);
		$Page->SetPager('default', '{prev}{list}{next}');
		return $Page;
	}




	public function check() {


		$p_list=I("post.product_id");



		$cart_list = unserialize ( cookie ( "product_cart" ) );





		foreach($cart_list as $k=>$v){

			if(in_array($k,$p_list)){


//				unset($cart_list[$k]);


				$order_list[]=$v;

			}




		}


		cookie ( "product_order_cart", serialize ( $order_list ),86400 );




		$this->success("结算成功,开始完善订单",U("checkout"));








	}


	
	public function index(){
		$map [uid] = $this->u[id];


		$map [modal]="product";

		if(I("get.status")){
			$map [status] =I("get.status");
		}
		else{
			$map [status] =array("neq" ,0);
		}
		$map [modal] =array("neq" ,"chongzhi");



		$count = $this->db->where ( $map )->count ();
		$page = $this->page ( $count, 20 );
		$data = $this->db->where ( $map )->limit ( $page->firstRow . ',' . $page->listRows )->order ( array (
				
				"id" => "DESC" 
		) )->select ();



		foreach($data as $k=>$v){



			$plist=D("OrderInfo")->where(array("order_sn"=>$v["order_sn"]))->select();

			$s=0;

			foreach($plist as $v){

				$p=D("Product")->get_one($v["product_id"]);

				$p["num"]=$v["num"];
				$s+=$v["num"];

				$data[$k]["plist"][]=$p;

			}
			$data[$k]["num"]=D("OrderInfo")->where(array("order_sn"=>$v["order_sn"]))->sum("num");


		}



		$this->assign ( "Page", $page->show ( 'Admin' ) );
		$this->assign ( "_list", $data );
	
		$this->display ();
	}


	public function checkout(){


		if(IS_POST)	{





			$orderlist=D("Product")->get_order_sum();



			if(!$_POST["shop_id"]){

				$this->error("请添加收货地址!");
			}


			$_POST[title]=$orderlist["title"];
			$_POST[order_sn]= date('ymd').substr(time(),-5).substr(microtime(),2,5);
			$_POST [uid] =$this->u['id'];
			$_POST [ip] =ip2long(get_client_ip());
			$_POST [status]=1;
			$_POST [modal]="product";




			$j=D("Users")->where(array("id"=>$this->u['id']))->getField("j_id");

			$_POST [j_id]=$j;


			$_POST [price_sum]=$orderlist["price_sum"];
			$_POST [qb_price_sum]=$orderlist["qb_price_sum"];



			if (D("Order")->create ()) {

				$id = D("Order")->add ();

				if ($id) {


					$this->_add_order_info($orderlist,$_POST);
//					$this->_set_cart($orderlist);


					session("j_id",null);



					$this->success ( "订单生成成,开始付款", U ( "pay",array("order_sn"=>$_POST[order_sn]) ) );
				} else {
					$this->error ( "订单生成失败！" );
				}

			} else {
				$this->error ( $this->db->getError () );
			}


		}
		else{



//			$yigou=D("Product")->jiancha();
//
//			if($yigou){
//
//				$this->redirect("checkout");
//			}


			cookie("address_self",get_now_url());


		$uid=$this->u[id];
		$user=D("Users")->where(array("id"=>$uid))->find();


			$cartlist=D("Product")->get_order_sum();



			if(!$cartlist[num]){
				redirect(U('/Home/Product/index'));
			}



//			$yhj_map["youxiao"]=array("egt",date("Y-m-d"));
//
//			$yhj_map["uid"]=$uid;
//			$yhj_map["is_use"]=0;
//			$yhj_map["manzu"]=array("elt",$cartlist["zong"]);
//
//
//
//			$yhjs=M("Yhj")->where($yhj_map)->order("price desc")->select();



		$this->assign ( "pts", $cartlist );


		$address=D("Address")->where(array("uid"=>$uid))->order("'default'  desc")->select();


		$this->assign("address",$address);

		$this->assign("default_address",$address[0]);

		$this->assign("score",$user[score]);

		$this->assign("orderlist",$cartlist);


		$this->display("");

		}

	}

	function _set_cart($orderlist){
		$cart_list = unserialize ( cookie ( "product_cart" ) );


		foreach ( $orderlist["data"] as $k => $t ) {

			unset($cart_list[$t["id"]]);

		}
		cookie ( "product_cart", serialize ( $cart_list ),86400 );
		cookie ( "product_order_cart", "" );
	}

	function _add_order_info($orderlist){


		foreach($orderlist["data"] as $k=>$v){




				$da=array(
					"order_sn"=>$_POST["order_sn"],
					"product_id"=>$v[id],
					"num"=>$v[num],
					"product_name"=>$v["d_title"],
				);

				D("OrderInfo")->add($da);




		}



	}


	function del(){

		$map["uid"]=$this->u[id];
		$map["order_sn"]=I('get.order_sn');

		$map["status"]=1;


		$s=$this->db->where($map)->delete();

		if($s){
			D("OrderInfo")->where(array("order_sn"=>I('get.order_sn')))->delete();



		}
		$this->success("操作成功");

	}

	function shouhuo(){

		$map["uid"]=$this->u[id];
		$map["order_sn"]=I('get.order_sn');

		$map["status"]=3;

		$data=array("status"=>4);

		$this->db->where($map)->save($data);

		$this->success("操作成功");

	}


	function pingjia(){






		if(IS_POST){

			$map["uid"]=$this->u[id];
			$map["order_sn"]=I('post.order_sn');

			$map["status"]=4;

			$data=$this->db->where($map)->find();


			if(!$data){

				$this->error("信息有误");

			}

			$_POST["uid"]=$this->u[id];

			$this->db->where($map)->save(array("status"=>5));


			if (D("OrderPingjia")->create ( $_POST )) {
				if (D("OrderPingjia")->add () !== false) {


					$this->db->where($map)->save(array("status"=>5));


					$this->success ( "评价成功！", U ( "show",array("order_sn"=>I('post.order_sn')) ) );
				} else {
					$this->error ( "评价失败！" );
				}
			} else {
				$this->error ( D("OrderPingjia")->getError () );
			}







		}else{


			$map["uid"]=$this->u[id];
			$map["order_sn"]=I('get.order_sn');

			$map["status"]=4;

			$data=$this->db->where($map)->find();


			if(!$data){

				$this->error("信息有误");

			}
			$this->assign("_data",$data);
			$this->display();
		}


	}

	function cui(){



		$map["order_sn"]=I('get.order_sn');

		$data["cui"]=1;
		$this->db->where($map)->save($data);


		$this->success("操作成功,我们会尽快给您发货。");


	}
	




	public function pay() {


		if(IS_POST){
			$order=D("Order")->where(array("order_sn"=>I('post.order_sn')))->find();
			$pay_type=I("post.pay_type");

			if($order[status]==1){




				if($pay_type=="weixin"){






					$this->success("正在跳转支付页面",U("User/Weixin/pay",array("order_sn"=>I('post.order_sn'))));


				}


				if($pay_type=="qianbao"){


					$sj_price=$order["qb_price_sum"];


					if($sj_price>$this->u["money"]){


						$this->error("钱包金额不足");
					}


					$data=array(

						"status"=>2,
						"pay_type"=>$pay_type,
						"sj_price_sum"=>$sj_price,
						"pay_time"=>time(),
					);

					$s=D("Order")->where(array("order_sn"=>$_POST["order_sn"]))->save($data);



					if($s){




						D("Users")->xiaofei($this->u[id],$sj_price);


						$yue=D("Users")->where(array("id"=>$this->u[id]))->getField("money");

						$data = array(
							"ip" => get_client_ip(),
							"inputtime" => time(),
							"money" =>$sj_price,
							"uid" => $order["uid"],
							"yue" => $yue,
							"type" => "消费"
						);

						D("QbLog")->add($data);
//
//
//						$content=array(
//
//							"first"=>array(
//								"value"=>"恭喜你购买成功!",
//							),
//							"keyword1"=>array(
//								"value"=>"钱包支付",
//							),
//
//							"keyword2"=>array(
//								"value"=>$order['title'],
//							),
//							"keyword3"=>array(
//								"value"=>$order['order_sn'],
//							),
//							"keyword4"=>array(
//								"value"=>"您购买的商品已经支付成功，查看详情了解更多信息",
//							),
//							"remark"=>array(
//								"value"=>"",
//							)
//
//
//						);
//
//
//
//
//
//
//
//
//						$ss=$this->wk->sendTplMsg($this->u["open_id"],"3Nt_aKpXXupHz9IiWTDpGYtd2g2f7DstkuO5gR_Oz7s",U('User/Order/show',array('order_sn'=>$order['order_sn'])),$content);





						$this->success("支付完成",U("Order/index"));

					}else{


						$this->error("结算失败");

					}



				}



			}else{

				$this->error("订单已失效或者已经支付",U('Home/Index/index'));
			}


		}
		else{

			$orders=D("Order")->where(array("order_sn"=>I('get.order_sn')))->find();




			if($orders[status]==1){

//				$cslist=D("OrderInfo")->where(array("order_sn"=>I('get.order_sn')))->select();
//
//
//				foreach($cslist as $k=>$v){
//
//					$v[title]=D("Product")->where(array("id"=>$v["product_id"]))->getField("title");
//					$orders[cslist][]=$v;
//
//				}



				$this->assign("orderlist",$orders);




				$this->display("");


			}else{
				$this->error("订单已经支付或者已失效",U('Home/Index/index'));
			}



		}








		//$this->display(":index");

	}


	public  function show(){
		$orders=D("Order")->where(array("order_sn"=>I('get.order_sn')))->find();



				$cslist=D("OrderInfo")->where(array("order_sn"=>I('get.order_sn')))->select();


				foreach($cslist as $k=>$v){

					$v[title]=D("Product")->where(array("id"=>$v["product_id"]))->getField("title");
					$orders[cslist][]=$v;

				}


		$pingjia=D("OrderPingjia")->where(array("order_sn"=>I("get.order_sn")))->find();

			$this->assign("orderlist",$orders);
			$this->assign("pingjia",$pingjia);




			$this->display("");


	}
    
    
    
    //返回订单价格
    public function jiesuan(){
    	
    	
    	$order=D("Order")->check_out(I('post.order_sn'),I("post.yhj_id"),I("post.if_jf"),$this->u[id]);
    	
    	
    	$this->ajaxReturn($order['price_sum']);
    	
    	
    	
    }


	//我的购买记录
	function log(){

		$uid=$this->u[id];

		$cslist=D("Xiaoshou")->where(array("uid"=>$uid))->select();



		$this->assign("logs",$cslist);
		$this->assign("nav","gouwu_log");


		$this->display("");



	}

	}
    
